<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-27 17:37
 * Modified Time: 2019-06-27 17:37
 * Version: 2.0
 */

namespace App\Modules\Order\Controllers;

use App\Exceptions\NotFoundException;
use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\MemberCard\Services\MemberCardService;
use App\Modules\Order\Services\OfflineService;
use App\Modules\Order\Services\OrderService;
use App\Modules\Order\Transformers\OrderListTransformer;
use App\Modules\Order\Validators\OfflineControllerValidator;
use Flower\Core\Http\BaseController;
use Flower\Core\Http\Request;

class OfflineController extends BaseController
{
	private $orderService;
	private $memberCardService;

	public function __construct(OrderService $orderService, MemberCardService $memberCardService)
	{
		$this->memberCardService = $memberCardService;
		$this->orderService = $orderService;
	}

	public function getRules()
	{
		return OfflineControllerValidator::validator();
	}


	/**
	 * 线下预约
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\NotFoundException
	 * @author Happy dong
	 * @modifiedTime 2019-08-03 13:46
	 */
	public function offlineAppointmentOrder(Request $request)
	{
		$res = $this->orderService->createAppointmentOrder($request->store_id, $request->barber_id, $request->member_id, $request->service_id, $request->appointment_time, $request->mobile, $request->payment, config('constant.appointment.offline'), $request->nickname, $request->remark);
		if (!$res) return $this->error('添加失败！');
		return $this->success('添加成功!');
	}



	/**
	 * 直接到店消费
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\NotFoundException
	 * @author Happy dong
	 * @modifiedTime 2019-08-11 22:10
	 */
	public function addOfflineOrder(Request $request)
	{
		$res = $this->orderService->addOffline(FLAuth::id(), $request->store_id, $request->service_id, $request->real_price, $request->is_appoint, $request->source, $request->member_id, $request->count, $request->change_reason, $request->remark, $request->member_card_id, $request->payment, $request->assistant_id);
		if (empty($res)) return $this->error('添加失败！');
		return $this->success('添加成功!');
	}


	/**
	 * 线下订单列表
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getOfflineOrderList(Request $request)
	{
		if (!$request->has('store_id')) {
			return $this->error("店铺Id参数缺失错误");
		}
		if ($request->has('date') && !strstr($request->date, '-')) {
			throw new NotFoundException(['msg' => 'date数据格式错误，请安装"2019-10"格式传递参数']);
		}

		$date = $request->date == date('Y-n') ? date('Y-m-d') : $request->date;
		$list = $this->orderService->searchOrderBarber(FLAuth::id(), $request->store_id, config('constant.orderStatus.confirm'), $request->page_size, $request->keyword, true, $date);
		$list = (new OrderListTransformer($list))->transform();
		$list = Utilities::dealPage($list);
		return $this->display($list);
	}

	/**
	 * 散客订单列表数据统计
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getOfflineStatistics(Request $request)
	{
		$data = $this->orderService->getOfflineStatistics(FLAuth::id(), $request->store_id, config('constant.orderStatus.done'), true, $request->date);
		return $this->display($data);
	}

}